//
//  QAViewCell.m
//  xpkc
//
//  Created by TopSageOSX on 15/2/28.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "QAViewCell.h"

@implementation QAViewCell{
//    UIImageView *flagView;
//    UILabel *question;
//    UILabel *time;
//    UILabel *state;
}

- (void)awakeFromNib {
  
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self creatCell];
    }
    return self;
}
- (void)creatCell{
    
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    CGRect flag_frame = CGRectMake(0, offsize/2, offsize, offsize);
    CGRect name_frame=CGRectMake(offsize*1/2, offsize/2+60, 150, 20);

    
    self.backgroundColor = BGColor;
    
    self.bigView = [[UIView alloc]init];
    
    self.bigView.backgroundColor = [UIColor whiteColor];
    self.bigView.layer.cornerRadius = 4;
    [self.contentView addSubview:self.bigView];
    
    
    self.myasktitle=[[UILabel alloc] initWithFrame:CGRectMake(offsize+10, offsize/2, SCREEN_WIDTH-offsize-10, offsize)];
    self.myasktitle.font=[UIFont systemFontOfSize:10];
    self.myasktitle.textColor=[UIColor lightGrayColor];
    [self.bigView addSubview:self.myasktitle];
    
    
    self.myname=[[UILabel alloc] initWithFrame:name_frame];
    self.myname.font=FONT_SYS_M;
    self.myname.textColor=[UIColor lightGrayColor];
    [self.bigView addSubview:self.myname];
    
    
    self.flagView = [[UIImageView alloc]initWithFrame:flag_frame];
    self.flagView.image = [UIImage imageNamed:@"question"];
    self.flagView.layer.cornerRadius = offsize/2;
    self.flagView.clipsToBounds = YES;
    [self.bigView addSubview:self.flagView];
    self.questionLabel = [[UILabel alloc]init];
  
    self.questionLabel.font = FONT_SYS_M;
    self.questionLabel.numberOfLines = 0;
    self.questionLabel.textColor = [UIColor lightGrayColor];
    [self.bigView addSubview:self.questionLabel];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font = FONT_SYS_XS;
    self.timeLabel.textColor = [UIColor lightGrayColor];
    [self.bigView addSubview:self.timeLabel];
    
    self.stateLabel = [[UILabel alloc]init];
    self.stateLabel.font = FONT_SYS_XS;
    self.stateLabel.textAlignment = NSTextAlignmentCenter;
    self.stateLabel.layer.cornerRadius = 2;
    [self.bigView addSubview:self.stateLabel];
    
    self.lineLabel = [[UIView alloc]init];
    
    self.lineLabel.backgroundColor = [UIColor lightGrayColor];
    [self.bigView addSubview:self.lineLabel];
    
    self.text = [[UILabel alloc]init];
    self.text.text = @"立即查看";
    self.text.textColor = [UIColor lightGrayColor];
    self.text.font = FONT_SYS_XS;
    [self.bigView addSubview:self.text];
    
    self.rightView = [[UIImageView alloc]init];
    
    self.rightView.image = [UIImage imageNamed:@"rightsign@2x"];
    [self.bigView addSubview:self.rightView];
    
    
}

-(void)configWithAskModel:(myAskModel *)askmodel{
    
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    
    

    
    CGRect bg_frame = CGRectMake(offsize/2, offsize/2, SCREEN_WIDTH-offsize, 120 -offsize );

    
    CGRect ques_frame = CGRectMake(offsize*3/2, offsize/2+15, bg_frame.size.width-offsize*2, 55);
    

    
    CGRect time_frame = CGRectMake(SCREEN_WIDTH-200, offsize/2+60 , 200, 20);

    CGRect state_frame = CGRectMake(SCREEN_WIDTH-60, offsize/2+60 , 40, 18);
    CGRect line_frame = CGRectMake(2, offsize/2+80, bg_frame.size.width-4, 1);
    CGRect text_frame = CGRectMake(offsize/2, offsize/2+80, 100, 20);
    CGRect right_frame = CGRectMake(bg_frame.size.width-15 , offsize/2+83 , 8, 12);
    
    
    NSMutableString * mutStr = [NSMutableString stringWithString:askmodel.myaskcontent];
    [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, askmodel.myaskcontent.length - 1)];
    
    [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length)];
    
    [mutStr replaceOccurrencesOfString:@"<br/>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, mutStr.length)];
    NSLog(@"%@",mutStr);

    CGSize contentSize = [mutStr sizeWithFont:FONT_SYS_M constrainedToSize:CGSizeMake(SCREEN_WIDTH-offsize - offsize * 2 , 2000)];
    if (contentSize.height > 55) {
        
        CGFloat bgHeight = contentSize.height;
        
        bg_frame = CGRectMake(offsize/2, offsize/2, SCREEN_WIDTH-offsize,120 -offsize+ bgHeight - 55);
        ques_frame = CGRectMake(offsize*3/2, offsize/2+15, bg_frame.size.width-offsize*2, bgHeight);
        time_frame = CGRectMake(SCREEN_WIDTH-200, offsize/2+60+ bgHeight - 55, 200, 20);
        state_frame = CGRectMake(SCREEN_WIDTH-60, offsize/2+60+ bgHeight - 55, 40, 18);
        line_frame = CGRectMake(2, offsize/2+80+ bgHeight - 55, bg_frame.size.width-4, 1);
        right_frame = CGRectMake(bg_frame.size.width-15 , offsize/2+83 + bgHeight - 55, 8, 12);
        text_frame = CGRectMake(offsize/2, offsize/2+80+ bgHeight - 55, 100, 20);
    }
    
    self.bigView.frame = bg_frame;
    self.questionLabel.frame = ques_frame;
    self.timeLabel.frame = time_frame;
    self.stateLabel.frame = state_frame;
    self.lineLabel.frame = line_frame;
    self.text.frame = text_frame;
    self.rightView.frame = right_frame;
    
    self.questionLabel.text = mutStr;
    self.timeLabel.text = askmodel.asktime;
    self.myname.text = askmodel.myname;
    self.myasktitle.text = askmodel.myasktitle;
    if (askmodel.ReplyList.count) {
        self.msgState=MsgStateRecive;
    }else{
        self.msgState = MsgStateWait;
    }
    
    
    
}

-(CGFloat)getHeightAskModel:(myAskModel *)askModel{
    
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    CGSize contentSize = [askModel.myaskcontent sizeWithFont:FONT_SYS_M constrainedToSize:CGSizeMake(SCREEN_WIDTH-offsize - offsize * 2 , 2000)];
    
    if (contentSize.height > 55) {
        
        return 120 + contentSize.height - 55;
    }
    
    return 120;
}

- (void)setMsgState:(MsgState)msgState{
    switch (msgState) {
        case MsgStateWait:
        {
            self.stateLabel.text = @"待回复";
            self.stateLabel.textColor = [UIColor whiteColor];
            self.stateLabel.backgroundColor = GreenColor;
            self.flagView.image = [UIImage imageNamed:@"question"];
        }
            break;
        case MsgStateRecive:
        {
            self.stateLabel.text = @"新回复";
            self.stateLabel.textColor = [UIColor whiteColor];
            self.stateLabel.backgroundColor = OrangeColor;
            self.flagView.image = [UIImage imageNamed:@"question"];
        }
            break;
        case MsgStateReaded:
        {
            self.stateLabel.text = @"已 读";
            self.stateLabel.textColor = [UIColor lightGrayColor];
            self.stateLabel.backgroundColor = [UIColor clearColor];
            self.flagView.image = nil;
        }
            break;
            
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
